<template>
	<view>
		<!-- 审核 -->
		<u-popup :show="shVisible" @close="close('discount_examine')" round="10" closeable>
			<view class="popup-box">
				<u-radio-group v-model="shForm.reviewPassed" placement="row" activeColor="#3B75FF">
					<u-radio :name="1" label="通过" :customStyle="{ marginRight: '40rpx' }"></u-radio>
					<u-radio :name="2" label="拒绝"></u-radio>
				</u-radio-group>
				<view class="textarea-box">
					<u-textarea v-model="shForm.remark" placeholder="输入审批意见"></u-textarea>
				</view>
				<view class="textarea-box"
					v-if="shForm.reviewPassed == 1 && oper_item.is_retrial != 3 && (userInfo.position_id == 1 || userInfo.position_id == 10)">
					<u-checkbox-group v-model="shForm.is_retrial" placement="column">
						<u-checkbox :customStyle="{marginBottom: '8px'}" label="是否需要总经理审核" :name="1">
						</u-checkbox>
					</u-checkbox-group>
				</view>
				<u-button type="primary" text="提交" color="#3B75FF" :customStyle="{ marginTop: '30rpx' }"
					@click="shSubmit"></u-button>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		name: "discountOperate",
		props: {
			value: {
				type: String,
				default: ''
			},
			oper_item: {
				type: Object,
				default: {}
			}
		},
		watch: {
			value(val) {
				let that = this;
				that.userInfo = uni.getStorageSync('userInfo')
				switch (val) {
					// 审核
					case 'discount_examine':
						this.shVisible = true;
						break;
						// 复审
					case 'discount_discountReviewPassed2':
						this.shVisible = true;
						break;
						// 作废
					case 'discount_discountInvalidation':
						uni.showModal({
							title: '提示',
							content: '确认作废该优惠审批吗？',
							showCancel: true,
							success(res) {
								if (res.confirm) {
									uni.showLoading({
										mask: true
									})
									that.$Apipc.discountInvalidation({
										discount_id: that.oper_item.id
									}).then(res => {
										console.log(res, '作废')
										uni.hideLoading()
										if (res.code == 200) {
											that.$emit('submit', that.value);
											that.close('discount_discountInvalidation');
										}
										uni.showToast({
											title: res.message,
											icon: res.code == 200 ? 'success' : 'error'
										})
									})
								}
							}
						})
						break;
					default:
				}
			}
		},
		data() {
			return {
				shVisible: false,
				shForm: {
					remark: "",
					quotation_id: "",
					reviewPassed: 1,
					is_retrial: []
				},
				userInfo: ""
			};
		},
		methods: {
			close(type) {
				switch (type) {
					case 'discount_examine':
						this.shForm = {
							remark: "",
							quotation_id: "",
							reviewPassed: 1,
							is_retrial: []
						}
						this.shVisible = false;
						break;
					default:
				}
				this.$emit('update:value', '');
			},
			shSubmit() {
				let shForm = this.shForm;
				console.log(shForm)
				if (!shForm.reviewPassed == 2 && !shForm.remark) {
					uni.showToast({
						title: '请输入审批意见',
						icon: 'error'
					})
					return;
				}
				if (shForm.reviewPassed == 2) {
					this.discountReviewRejected({
						discount_id: this.oper_item.id,
						reason_refusal: shForm.remark
					});
				} else {
					// 通过
					let obj = {
						discount_id: this.oper_item.id,
						reason_refusal: shForm.remark
					};
					if (this.oper_item.is_retrial != 3 && (this.userInfo.position_id == 1 || this.userInfo.position_id ==
							10)) {
						obj.is_retrial = shForm.is_retrial.length ? 3 : 0;
					}
					this.discountReviewPassed(obj);
				}
			},
			// 拒绝审核
			discountReviewRejected(obj) {
				uni.showLoading({
					mask: true
				})
				this.$Apipc.discountReviewRejected(obj).then(res => {
					console.log(res, "拒绝审核")
					uni.hideLoading()
					if (res.code == 200) {
						this.$emit('submit', this.value);
						this.close('discount_examine');
					}
					uni.showToast({
						icon: res.code == 200 ? 'success' : 'error',
						title: res.message
					})
				})
			},
			// 通过审核
			discountReviewPassed(obj) {
				uni.showLoading({
					mask: true
				})
				this.$Apipc.discountReviewPassed(obj).then(res => {
					console.log(res, "审核通过")
					uni.hideLoading()
					if (res.code == 200) {
						this.$emit('submit', this.value);
						this.close('discount_examine');
					}
					uni.showToast({
						icon: res.code == 200 ? 'success' : 'error',
						title: res.message
					})
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.popup-title {
		height: 46px;
		border-bottom: 2rpx solid #efefef;
		text-align: center;
		line-height: 46px;
		font-size: 30rpx;
	}

	.popup-box {
		padding: 30rpx 20rpx 20rpx;

		.textarea-box {
			margin-top: 30rpx;
		}
	}
</style>